<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="" prop="teamId">
      <el-input v-model="dataForm.teamId" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="进攻效率" prop="attackEfficiency">
      <el-input v-model="dataForm.attackEfficiency" placeholder="进攻效率"></el-input>
    </el-form-item>
    <el-form-item label="防守效率" prop="defenseEfficiency">
      <el-input v-model="dataForm.defenseEfficiency" placeholder="防守效率"></el-input>
    </el-form-item>
    <el-form-item label="真实命中率" prop="trueShot">
      <el-input v-model="dataForm.trueShot" placeholder="真实命中率"></el-input>
    </el-form-item>
    <el-form-item label="场均回合" prop="rounds">
      <el-input v-model="dataForm.rounds" placeholder="场均回合"></el-input>
    </el-form-item>
    <el-form-item label="失误率" prop="errorRate">
      <el-input v-model="dataForm.errorRate" placeholder="失误率"></el-input>
    </el-form-item>
    <el-form-item label="罚球率" prop="freeRate">
      <el-input v-model="dataForm.freeRate" placeholder="罚球率"></el-input>
    </el-form-item>
    <el-form-item label="利用对方失误得分" prop="scoreByerror">
      <el-input v-model="dataForm.scoreByerror" placeholder="利用对方失误得分"></el-input>
    </el-form-item>
    <el-form-item label="对手命中率" prop="opposeShot">
      <el-input v-model="dataForm.opposeShot" placeholder="对手命中率"></el-input>
    </el-form-item>
    <el-form-item label="赛季" prop="season">
      <el-input v-model="dataForm.season" placeholder="赛季"></el-input>
    </el-form-item>
    <el-form-item label="常规赛/季后赛/总决赛" prop="match">
      <el-input v-model="dataForm.match" placeholder="常规赛/季后赛/总决赛"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          teamId: '',
          attackEfficiency: '',
          defenseEfficiency: '',
          trueShot: '',
          rounds: '',
          errorRate: '',
          freeRate: '',
          scoreByerror: '',
          opposeShot: '',
          season: '',
          match: ''
        },
        dataRule: {
          teamId: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          attackEfficiency: [
            { required: true, message: '进攻效率不能为空', trigger: 'blur' }
          ],
          defenseEfficiency: [
            { required: true, message: '防守效率不能为空', trigger: 'blur' }
          ],
          trueShot: [
            { required: true, message: '真实命中率不能为空', trigger: 'blur' }
          ],
          rounds: [
            { required: true, message: '场均回合不能为空', trigger: 'blur' }
          ],
          errorRate: [
            { required: true, message: '失误率不能为空', trigger: 'blur' }
          ],
          freeRate: [
            { required: true, message: '罚球率不能为空', trigger: 'blur' }
          ],
          scoreByerror: [
            { required: true, message: '利用对方失误得分不能为空', trigger: 'blur' }
          ],
          opposeShot: [
            { required: true, message: '对手命中率不能为空', trigger: 'blur' }
          ],
          season: [
            { required: true, message: '赛季不能为空', trigger: 'blur' }
          ],
          match: [
            { required: true, message: '常规赛/季后赛/总决赛不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/team/teamadvanced/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.teamId = data.teamAdvanced.teamId
                this.dataForm.attackEfficiency = data.teamAdvanced.attackEfficiency
                this.dataForm.defenseEfficiency = data.teamAdvanced.defenseEfficiency
                this.dataForm.trueShot = data.teamAdvanced.trueShot
                this.dataForm.rounds = data.teamAdvanced.rounds
                this.dataForm.errorRate = data.teamAdvanced.errorRate
                this.dataForm.freeRate = data.teamAdvanced.freeRate
                this.dataForm.scoreByerror = data.teamAdvanced.scoreByerror
                this.dataForm.opposeShot = data.teamAdvanced.opposeShot
                this.dataForm.season = data.teamAdvanced.season
                this.dataForm.match = data.teamAdvanced.match
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/team/teamadvanced/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'teamId': this.dataForm.teamId,
                'attackEfficiency': this.dataForm.attackEfficiency,
                'defenseEfficiency': this.dataForm.defenseEfficiency,
                'trueShot': this.dataForm.trueShot,
                'rounds': this.dataForm.rounds,
                'errorRate': this.dataForm.errorRate,
                'freeRate': this.dataForm.freeRate,
                'scoreByerror': this.dataForm.scoreByerror,
                'opposeShot': this.dataForm.opposeShot,
                'season': this.dataForm.season,
                'match': this.dataForm.match
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
